import DEFINE = require('./../DEFINE');
export  = CommonModel;

/**
 *
 * @author 
 *
 */
class CommonModel {
    public static SPREAD_NONE: number=0;   // 0表示不需要扩展／1表示扩展／2表示收拢
    public static SPREAD_EXPAND: number=1;
    public static SPREAD_COLLAPSE: number=2;

    public id: number=DEFINE.ID_NONE; // id
    public value: string="";             // value
    
    public spread: number=CommonModel.SPREAD_NONE;            // 扩展状态
    public level: number=0;                                  // 0表示顶级，每向下一级就加一
    
    public child: Array<CommonModel>=[];                     // 子对象，有子对象表示树状结构，否则表示平铺结构
    
    public select: Boolean=false;   // true表示选中
    
    public constructor(id: number=0,
        value: string="",
        spread: number=CommonModel.SPREAD_NONE,
        level: number=0,
        select: Boolean=false,
        childs: Array<CommonModel>=[]) {
        this.id=id;
        this.value=value;
        this.spread=spread;
        this.level=level;
        this.select=select;
        this.child=childs;
    }
    // 转成json字符串
    public toJson(): string {
        // 去掉父json字符串的头尾
        var superJson="";

        var ret='{';
        ret+=superJson;
                    
        // id
        ret+=',';
        ret+='"id":';
        ret+=this.id;
        
        // value
        ret+=',';
        ret+='"value":';
        ret+=this.value;
        
        // spread
        ret+=',';
        ret+='"spread":';
        ret+=this.spread;
        
        // level
        ret+=',';
        ret+='"level":';
        ret+=this.level;
        
        // select
        ret+=',';
        ret+='"select":';
        ret+=this.select;
        
        
        // children
        ret+=',';
        ret+='"child":';
        ret+='[';
        for(var i=0;i<this.child.length;i++) {
            if(i>0) {
                ret+=',';
            }
            ret+=this.child[i].toJson();
        }
        ret+=']';


        ret+='}';
        return ret;
    }
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj) {
        this.id=jsonObj.id;
        this.value=jsonObj.value;
        this.spread=jsonObj.spread;
        this.level=jsonObj.level;
        this.select=jsonObj.select;

        for(var i=0;i<jsonObj.child.length;i++) {
            var one: CommonModel=new CommonModel();
            if(one) {
                one.fromJson(jsonObj.child[i]);
                this.child.push(one);
            }
        }
    }

    public static from(id: number,
        value: string,
        spread: number=CommonModel.SPREAD_NONE,
        level: number=0,
        select: Boolean=false,
        childs: Array<CommonModel>=[]): CommonModel {
        return new CommonModel(id,value,spread,level,select,childs);
    }

    public addChild(c: CommonModel) {
        this.child.push(c);
    }
	
    // 对“树”执行抹平操作
    public flattenTree(): Array<CommonModel> {
        var ret: Array<CommonModel>=[];
        // 收集自身
        var self: CommonModel=CommonModel.from(this.id,this.value,this.spread,this.level,this.select,[]);
        ret.push(self);
        
        // 收集子
        if(this.spread==CommonModel.SPREAD_EXPAND) {
            for(var i=0;i<this.child.length;i++) {
                var one: CommonModel=this.child[i];
                var s: Array<CommonModel>=one.flattenTree();
                ret=ret.concat(s);
            }
        }


        return ret;
    }
    
    // 打开/收拢某一个model
    public doSpread(itemId: number,spread: Boolean=true): Boolean {
        if(spread) {
            if(this.id==itemId&&this.spread==CommonModel.SPREAD_COLLAPSE) {
                this.spread=CommonModel.SPREAD_EXPAND;
                return true;
            } else {
                // 处理子
                for(var i=0;i<this.child.length;i++) {
                    var one: CommonModel=this.child[i];
                    if(one.doSpread(itemId,spread))
                        return true;
                }
            }

        } else {
            if(this.id==itemId&&this.spread==CommonModel.SPREAD_EXPAND) {
                this.spread=CommonModel.SPREAD_COLLAPSE;
                return true;
            } else {
                // 处理子
                for(var i=0;i<this.child.length;i++) {
                    var one: CommonModel=this.child[i];
                    if(one.doSpread(itemId,spread))
                        return true;
                }
            }
        }

        return false;
    }
    // 转换成easyui的tree的data结构
    //      {
    //                text: 'Item1',
    //                state: 'closed',
    //                attributes: {
    //                      id:1,
    //                      level:0
    //                },
    //                children: [{
    //                              text: 'Item11'
    //                          },{
    //                              text: 'Item12'
    //                }]
    //       }
    public toEasyUITree(desc: boolean=false): Object {
        var ret: any={};
        ret.text=this.value;

        ret.iconCls="icon-save";
        ret.attributes={};
        ret.attributes.id=this.id;
        ret.attributes.level=this.level;

        var len=this.child.length;
        if(len>0) {
            ret.state=this.spread==CommonModel.SPREAD_EXPAND? 'open':'closed';
            ret.children=[];
            if(desc) {
                for(var i=len-1;i>=0;i--) {
                    var c: CommonModel=this.child[i];
                    ret.children.push(c.toEasyUITree(desc));
                }
            } else {
                for(var i=0;i<len;i++) {
                    var c: CommonModel=this.child[i];
                    ret.children.push(c.toEasyUITree(desc));
                }
            }

        }

        return ret;
    }


//SJG_MARK_APPEND--------------------


}//SJG_MARK_END-------------------------